package people.paper.tv.activity.home.modual.rmrb.view.impl;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.NewRecycleAdapter;
import android.support.v7.widget.OrientationHelper;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.skyworth.ui.newrecycleview.NewRecycleAdapterItem;
import com.skyworth.ui.newrecycleview.NewRecycleLayout;
import com.skyworth.ui.newrecycleview.OnBoundaryListener;
import com.skyworth.ui.newrecycleview.OnItemClickListener;
import com.skyworth.ui.newrecycleview.OnItemFocusChangeListener;
import com.skyworth.util.Util;

import java.util.ArrayList;
import java.util.List;

import people.paper.tv.R;
import people.paper.tv.activity.home.modual.rmrb.view.data.DailyData;
import people.paper.tv.utils.ui.Common;

public class DailyListView extends FrameLayout implements OnBoundaryListener,
        OnItemFocusChangeListener, OnItemClickListener {
    final String TAG = "YWYLViewImpl";
    NewRecycleLayout gridView;
    DailyData dailyData;
    List<DailyData.LayoutData> gridLayoutDatas;
    List<DailyData.ReportData> gridDatas;
    NewRecycleAdapter gridAdapter, gridLayoutAdapter;
    IRMRBViewCallBack viewCallBack;
    LayoutCallback layoutCallback;
    RMRBLayout parentLayout;
    int curLayout = 0, oldLayout = 0, curPlayLayout = -1, oldPosition = 0, curPlayPosition = -1;
    View focus;
    boolean isLayout;
    int width;

    public DailyListView(@NonNull Context context, boolean layout) {
        super(context);
        isLayout = layout;
        setClipChildren(false);
        setClipToPadding(false);
        focus = new View(context);
        focus.setFocusable(true);
        focus.setFocusableInTouchMode(true);
        focus.setVisibility(GONE);
        addView(focus);
        focus.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                return true;
            }
        });
    }

    private void createNewsList(){
        gridView = new NewRecycleLayout(getContext(), 1, OrientationHelper.VERTICAL);
        if(isLayout){
            width = Util.Div(270);
        }else {
            width = Util.Div(636);
        }
        LayoutParams gridViewParams = new LayoutParams(width, Util.Div(840));
        gridView.setItemSpace(Util.Div(0),Util.Div(0));
        addView(gridView, gridViewParams);
        gridView.setMiddleScroll(true);
        gridView.setmBoundaryListener(this);
        gridView.setmItemFocusChangeListener(this);
        gridView.setmItemClickListener(this);
        gridView.setClipChildren(false);
        gridView.setClipToPadding(false);
    }

    public void setParentViewCallback(IRMRBViewCallBack view){
        viewCallBack = view;
        parentLayout = (RMRBLayout)view.getParentView();
    }

    public void setLayoutCallback(LayoutCallback layout){
        layoutCallback = layout;
    }

    public void refresh(DailyData datas){
        dailyData = datas;
        if(gridView == null) {
            createNewsList();
        }
        curLayout = 0;
        oldLayout = 0;
        if(isLayout){
            setLayoutAdapter(dailyData.layoutDatas);
        }else {
            setAdapter(dailyData.layoutDatas.get(0).reportDataList, false);
        }
    }

    public void refreshAdapter(){
        setAdapter(dailyData.layoutDatas.get(curLayout).reportDataList, false);
    }

    protected void setLayoutAdapter(final List<DailyData.LayoutData> datas){
        if(datas == null || datas.size() <= 0)
            return;
        gridLayoutDatas = new ArrayList<>();
        int size = datas.size();
        for(int i = 0; i < size; i++)
        {
            gridLayoutDatas.add(datas.get(i));
        }
        gridLayoutAdapter = new NewRecycleAdapter<DailyData.LayoutData>(gridLayoutDatas,1) {
            @Override
            public NewRecycleAdapterItem<DailyData.LayoutData> onCreateItem(Object type) {
                return new LayoutItemView(getContext(), isLayout);
            }
        };
        gridView.setRecyclerAdapter(gridLayoutAdapter);
    }

    protected void setAdapter(final List<DailyData.ReportData> datas, final boolean needFocus){
        if(datas == null || datas.size() <= 0)
            return;
        gridDatas = new ArrayList<>();
        int size = datas.size();
        for(int i = 0; i < size; i++)
        {
            gridDatas.add(datas.get(i));
        }
        if(curLayout == curPlayLayout){
            gridDatas.get(curPlayPosition).isCurSelected = true;
        }
        gridAdapter = new NewRecycleAdapter<DailyData.ReportData>(gridDatas,1) {
            @Override
            public NewRecycleAdapterItem<DailyData.ReportData> onCreateItem(Object type) {
                return new DailyItemView(getContext(), isLayout);
            }
        };
        gridView.setRecyclerAdapter(gridAdapter);
        gridView.post(new Runnable() {
            @Override
            public void run() {
                if(gridView.getChildAt(0) != null) {
                    if(needFocus){
                        gridView.getChildAt(0).requestFocus();
                        focus.setVisibility(GONE);
                    }
                }
            }
        });
    }

    public void refreshCurLayout(int layoutId){
        curLayout = layoutId;
    }

    public void refreshCurLayoutId(int position){
        if(gridLayoutDatas != null && gridLayoutDatas.size() > oldLayout)
            gridLayoutDatas.get(oldLayout).isCurSelected = false;
        if(gridLayoutDatas != null && gridLayoutDatas.size() > position)
            gridLayoutDatas.get(position).isCurSelected = true;
        gridView.notifyItemChanged(oldLayout);
        gridView.notifyItemChanged(position);
        oldLayout = position;
        curLayout = position;
        if(gridView != null){
            gridView.smoothScrollToPosition(position);
        }
    }

    public void refreshCurPlayId(int layoutid, int position){
        if(layoutid == curLayout){
            if(layoutid == curPlayLayout){
                if(curPlayPosition >= 0 && gridDatas != null && gridDatas.size() > curPlayPosition)
                    gridDatas.get(curPlayPosition).isCurSelected = false;
                gridView.notifyItemChanged(curPlayPosition);
            }else {
                if(curPlayLayout >= 0 && curPlayPosition >= 0)
                    dailyData.layoutDatas.get(curPlayLayout).reportDataList.get(curPlayPosition).isCurSelected = false;
            }
            if(gridDatas != null && gridDatas.size() > position)
                gridDatas.get(position).isCurSelected = true;
            gridView.notifyItemChanged(position);
            curPlayPosition = position;
            if(gridView != null){
                gridView.smoothScrollToPosition(position);
            }
        }else {
            if(curPlayLayout >= 0 && curPlayPosition >= 0)
                dailyData.layoutDatas.get(curPlayLayout).reportDataList.get(curPlayPosition).isCurSelected = false;
            dailyData.layoutDatas.get(layoutid).reportDataList.get(position).isCurSelected = true;
        }
        curPlayLayout = layoutid;
        curPlayPosition = position;
    }

    public boolean changeFocus() {
        if(gridView != null && gridView.getChildItemCount() > 0){
            View view = gridView.getViewByPosition(gridView.getFirstVisiblePosition());
            if(view != null) {
                view.requestFocus();
                return true;
            }
        }
        return false;
    }

    public int getChild(){
        if(gridView == null)
            return 0;
        else
            return gridView.getChildItemCount();
    }

    public void setPlaceFocus(){
        int currentId;
        if(isLayout){
            currentId = curLayout;
        }else {
            currentId = curPlayPosition;
        }
        ItemView view = (ItemView) gridView.getItemByPosition(currentId);
        if(view != null)
            view.requestFocus();
        else{
            ItemView view1 = (ItemView) gridView.getItemByPosition(0);
            if(view1 != null)
                view1.requestFocus();
        }
   }

    public void setAudioPause(){
//        viewCallBack.pauseAudio();
    }

    @Override
    public boolean onLeftBoundary(View view, int i) {
        if(!isLayout){
            return parentLayout.onListBoundaryKey(isLayout, KeyEvent.KEYCODE_DPAD_LEFT);
        }
        return false;
    }

    @Override
    public boolean onTopBoundary(View view, int i) {
        Log.i("RMRBViewImpl onNewsLoad", "RMRBViewImpl onTopBoundary:"+i+"; "+curLayout);
        if(isLayout){
            viewCallBack.changeFocus();
        }else {
            if(curLayout == 0){
                viewCallBack.changeFocus();
            }else {
                curLayout --;
                focus.setVisibility(VISIBLE);
                focus.requestFocus();
                setAudioPause();
                layoutCallback.refreshLayoutTitle(curLayout);
                setAdapter(dailyData.layoutDatas.get(curLayout).reportDataList, true);
            }
        }
        return true;
    }

    @Override
    public boolean onDownBoundary(View view, int i) {
        if(!isLayout){
            if(dailyData.layoutDatas.size() > curLayout+1){
                curLayout ++;
                focus.setVisibility(VISIBLE);
                focus.requestFocus();
                setAudioPause();
                layoutCallback.refreshLayoutTitle(curLayout);
                setAdapter(dailyData.layoutDatas.get(curLayout).reportDataList, true);
            }
        }
        return true;
    }

    @Override
    public boolean onRightBoundary(View view, int i) {
        if(isLayout){
            parentLayout.onListBoundaryKey(isLayout, KeyEvent.KEYCODE_DPAD_RIGHT);
        }else {
            viewCallBack.nextFragment();
        }
        return true;
    }

    @Override
    public boolean onOtherKeyEvent(View view, int i, int i1) {
        Log.i("RMRBViewImpl onNewsLoad", "RMRBViewImpl onOtherKeyEvent:"+i+"; "+i1);
        return parentLayout.onKey(view, i1, new KeyEvent(KeyEvent.ACTION_DOWN, i1));
    }

    @Override
    public void focusChange(View view, int i, boolean hasFocus) {
        if(isLayout && hasFocus){
            setAudioPause();
            curLayout = i;
            layoutCallback.refreshLayoutTitle(i);
            parentLayout.onListClick(isLayout);
        }
        ItemView itemView = (ItemView)view;
        itemView.onFocusChange(hasFocus);
    }

    @Override
    public void click(View v, int position) {
        if(isLayout){
        }else {
            DailyItemView itemView = (DailyItemView)v;
            itemView.onClickAction();
        }
    }
}
